package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.ClassBean;
import com.project.bean.StudentBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ClassServiceImpl implements IClassService {
    @Autowired
    private IClassMapper classMapper;
    @Autowired
    private IStudentMapper studentMapper;
    @Override
    public List<ClassBean> findAll() {
        return classMapper.selectList(null);
    }

    @Override
    public void add(ClassBean classObj) {
        classMapper.insert(classObj);
    }

    @Override
    public boolean del(int classId) {
        QueryWrapper<StudentBean> qw = new QueryWrapper<>();
        qw.eq("fk_classId",classId);
        Integer num = studentMapper.selectCount(qw);
        if(num == 0){
            classMapper.deleteById(classId);
            return true;
        }

        return false;
    }
}
